<template>
	<div>
		<div v-if="notEmpty(infoData)">
			<DetailsApprove
				ref="DetailsApprove"
				v-model="infoData"
				:state="state"
				type="preview"
				env="flowable"></DetailsApprove>
		</div>
	</div>
</template>

<script>
	// import DetailsApprove from "@/views/quota/forecast/chunk/details_approve/index.vue";
	import DetailsApprove from '@/views/quota/forecast/details/main_approve.vue';
	import * as apis from '@/api/quota/apply/edit/api.js';
	export default {
		name: 'ForecastDetailsApprove',
		components: { DetailsApprove },
		props: {
			value: {
				required: false,
			},
			state: {
				required: false,
			},
			type: {
				required: false,
			},
		},

		data() {
			return {};
		},

		computed: {
			infoData: {
				get() {
					return this.value;
				},
				set(val) {
					this.$emit('input', val);
				},
			},
		},
		provide() {
			return {};
		},
		created() {
			//
			// if (this.Empty(this.infoData)) {
			// 	this.setFormData();
			// }
		},
		methods: {
			// 假如上层给的对象不是null,undefined,'',可能为'{}'传了进来，调这个借口获取详情重新赋值
			setFormData() {
				console.log('setFormData');
				let id = this.$route.query.processId;
				if (!id) return;
				apis.DetailsByProcessId(id).then(res => {
					let data = res.data;
					data?.productList?.forEach(product => {
						try {
							if (this.notEmpty(product?.calcSnapshoot)) {
								product.calcSnapshoot = JSON.parse(
									product.calcSnapshoot,
								);
							}
						} catch (error) {
							console.warn(error);
						}
					});
					this.infoData = data;
				});
			},
			flow_submit() {
				console.log('flow_submit');
				this.$emitter.emit('setchangeState');
			},
			valid() {
				return new Promise(async resolve => {
					let result = true;
					let refs = Object.values(this.$refs);
					for (let index = 0; index < refs?.length; index++) {
						const ref = refs[index];
						if (
							Object.prototype.toString.call(ref) ==
							'[object Array]'
						) {
							for (let i = 0; i < ref.length; i++) {
								const element = ref[i];
								if (element?.valid) {
									let rc = await element?.valid?.();
									result = result && rc;
								}
								if (element?.validate) {
									let rf = await this._valid(ref);
									result = result && rf;
								}
								if (element?.form_validate) {
									let rf = await this._valid(
										ref,
										'form_validate',
									);
									result = result && rf;
								}
							}
						} else {
							if (ref?.valid) {
								let rc = await ref?.valid?.();
								result = result && rc;
							}
							if (ref?.validate) {
								let rf = await this._valid(ref);
								result = result && rf;
							}
							if (ref?.form_validate) {
								let rf = await this._valid(
									ref,
									'form_validate',
								);
								result = result && rf;
							}
						}
					}

					resolve(result);
				});
			},
		},
	};
</script>

<style lang="scss" scoped></style>
